import React from 'react';
import styled, { keyframes } from 'styled-components';
import dayjs from 'dayjs';

const Card = styled.div`
  background: ${props => props.theme.cardBackground};
  border: 1px solid ${props => props.theme.border};
  border-radius: 16px;
  padding: 1.8rem;
  display: flex;
  flex-direction: column;
  gap: 1.2rem;
  position: relative;
  overflow: hidden;
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  box-shadow: 0 4px 20px ${props => props.theme.shadow};
  transition: all 0.3s ease;
  height: fit-content;
  margin: 0 0 24px 0;

  &::before {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    padding: 1px;
    background: linear-gradient(
      135deg,
      rgba(255, 255, 255, 0.1),
      rgba(255, 255, 255, 0.05)
    );
    -webkit-mask: linear-gradient(#000 0 0) content-box,
      linear-gradient(#000 0 0);
    -webkit-mask-composite: xor;
    mask-composite: exclude;
    pointer-events: none;
  }

  &:hover {
    transform: translateY(-4px);
    background: ${props => props.theme.hover};
    box-shadow: 0 8px 30px ${props => props.theme.shadow};

    &::before {
      background: linear-gradient(
        135deg,
        rgba(255, 255, 255, 0.2),
        rgba(255, 255, 255, 0.1)
      );
    }
  }
`;

const popIn = keyframes`
  0% {
    opacity: 0;
    transform: scale(0.6) translateY(10px);
  }
  70% {
    transform: scale(1.1) translateY(0);
  }
  100% {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
`;

const fadeOut = keyframes`
  0% {
    opacity: 1;
    transform: scale(1);
  }
  100% {
    opacity: 0;
    transform: scale(0.8);
  }
`;

const fadeInUp = keyframes`
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
`;

const fadeOutDown = keyframes`
  from {
    opacity: 1;
    transform: translateY(0);
  }
  to {
    opacity: 0;
    transform: translateY(-10px);
  }
`;

const CopyNotification = styled.div`
  position: absolute;
  top: 20px;
  left: 0;
  right: 0;
  margin: 0 auto;
  width: 240px;
  background: ${props => props.theme.notificationBackground};
  color: #FFFFFF;
  padding: 8px 32px;
  border-radius: 50px;
  font-size: 0.9rem;
  font-weight: 500;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 1000;
  justify-content: center;
  animation: 
    ${fadeInUp} 0.3s ease forwards,
    ${fadeOut} 0.3s ease forwards 1.5s;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);

  svg {
    width: 14px;
    height: 14px;
  }

  &::before {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    padding: 1px;
    background: linear-gradient(
      135deg,
      rgba(255, 255, 255, 0.3),
      rgba(255, 255, 255, 0.1)
    );
    -webkit-mask: 
      linear-gradient(#fff 0 0) content-box, 
      linear-gradient(#fff 0 0);
    -webkit-mask-composite: xor;
    mask-composite: exclude;
    pointer-events: none;
  }
`;

const Content = styled.div`
  position: relative;
  cursor: pointer;
  color: ${props => props.theme.text};
  line-height: 1.6;
  font-size: 0.95rem;
  letter-spacing: 0.3px;
  padding: 1rem;
  border-radius: 12px;
  transition: all 0.3s ease;
  
  p {
    position: relative;
    z-index: 1;
    display: inline;
    background-image: linear-gradient(
      90deg,
      ${props => props.theme.text},
      ${props => props.theme.text}
    );
    background-size: 0% 1px;
    background-repeat: no-repeat;
    background-position: left 100%;
    transition: background-size 0.3s ease;
  }

  &:hover {
    background: ${props => props.theme.hover};
    transform: translateY(-2px);

    p {
      background-size: 100% 1px;
    }
  }

  &:active {
    transform: translateY(0);
    background: ${props => props.theme.active};
  }
`;

const LinkContainer = styled.div`
  padding: 0.8rem;
  border-radius: 12px;
  background: ${props => props.theme.background};
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid ${props => props.theme.border};
  transition: all 0.3s ease;
  
  &:hover {
    background: ${props => props.theme.hover};
  }
`;

const Link = styled.a`
  position: relative;
  color: ${props => props.theme.link};
  text-decoration: none;
  font-size: 0.9rem;
  word-break: break-all;
  display: block;
  transition: all 0.3s ease;

  &::after {
    content: '';
    position: absolute;
    bottom: -2px;
    left: 0;
    width: 100%;
    height: 1px;
    background: linear-gradient(90deg, 
      ${props => props.theme.link}, 
      ${props => props.theme.linkSecondary}
    );
    transform: scaleX(0);
    transform-origin: right;
    transition: transform 0.3s ease;
  }

  &:hover {
    color: ${props => props.theme.linkSecondary};

    &::after {
      transform: scaleX(1);
      transform-origin: left;
    }
  }
`;

const Footer = styled.div`
  display: flex;
  align-items: center;
  margin-top: 0.8rem;
  opacity: 0.7;
  transition: opacity 0.3s ease;

  &:hover {
    opacity: 1;
  }
`;

const Timestamp = styled.span`
  color: ${props => props.theme.textSecondary};
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  letter-spacing: 0.3px;
  font-weight: 300;
  
  &::before {
    content: '';
    display: inline-block;
    width: 3px;
    height: 3px;
    margin-right: 6px;
    background: currentColor;
    border-radius: 50%;
    opacity: 0.5;
  }
`;

const DeleteButton = styled.button`
  position: absolute;
  top: 12px;
  right: 12px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: ${props => props.theme.background};
  border: 1px solid ${props => props.theme.border};
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease-in-out;
  z-index: 1;

  &:hover {
    background: ${props => props.theme.hover};
    transform: scale(1.1);
  }

  &::before,
  &::after {
    content: '';
    position: absolute;
    width: 12px;
    height: 2px;
    background-color: ${props => props.theme.text};
    transform: rotate(45deg);
  }

  &::after {
    transform: rotate(-45deg);
  }
`;

const DomainLabel = styled.div`
  position: absolute;
  top: 12px;
  left: 12px;
  background: ${props => props.theme.textPrimary}15;
  color: ${props => props.theme.textPrimary};
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 500;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 1;
  transition: all 0.3s ease;

  &:hover {
    background: ${props => props.theme.textPrimary}25;
  }
`;

const PostCard = ({ post, onDelete, theme }) => {
  const [showCopyNotification, setShowCopyNotification] = React.useState(false);

  const truncateContent = (content) => {
    if (content.length > 200) {
      return content.slice(0, 200) + '...';
    }
    return content;
  };

  const getDomain = (url) => {
    try {
      const domain = new URL(url).hostname;
      return domain.replace(/^www\./, '');
    } catch {
      return '';
    }
  };

  const handleCopyContent = async () => {
    try {
      await navigator.clipboard.writeText(post.content);
      setShowCopyNotification(true);
      setTimeout(() => {
        setShowCopyNotification(false);
      }, 1500);
    } catch (err) {
      console.error('Failed to copy text:', err);
    }
  };

  const handleDelete = (e) => {
    e.stopPropagation();
    onDelete(post.id);
  };

  return (
    <Card theme={theme}>
      <DomainLabel theme={theme}>{getDomain(post.link)}</DomainLabel>
      {post.editable && (
        <DeleteButton theme={theme} onClick={handleDelete} aria-label="Delete post" />
      )}
      {showCopyNotification && (
        <CopyNotification theme={theme}>
          <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
          </svg>
          已复制到剪贴板
        </CopyNotification>
      )}
      <Content theme={theme} onClick={handleCopyContent}>
        <p>{truncateContent(post.content)}</p>
      </Content>
      <LinkContainer theme={theme}>
        <Link theme={theme} href={post.link} target="_blank" rel="noopener noreferrer">
          {post.link}
        </Link>
      </LinkContainer>
      <Footer theme={theme}>
        <Timestamp theme={theme}>mark · {dayjs(post.createAt).format('YYYY-MM-DD')}</Timestamp>
      </Footer>
    </Card>
  );
};

export default PostCard;
